package com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.beans.Admin;
import com.beans.Company;
import com.beans.Position;
import com.beans.UPHistory;
import com.beans.User;
import com.service.CompanyManager;
import com.service.UserManager;
import com.util.Const;
import com.vo.Page;

public class PositionController extends UniversalController {
	private UserManager userManager;
	private CompanyManager companyManager;

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public CompanyManager getCompanyManager() {
		return companyManager;
	}

	public void setCompanyManager(CompanyManager companyManager) {
		this.companyManager = companyManager;
	}

	public ModelAndView positionForm(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		Map map = new HashMap();
		Position p = new Position();
		if (id != null && !"".equals(id)) {
			p = (Position) getUniversalManager().findByID(Position.class,
					Integer.parseInt(id));
		}
		else {
			p.setDays(30);
			p.setId(0);
			p.setPersons(0);
			p.setWages(0);
		}
		map.put("o", p);
		return new ModelAndView("company/positionForm", map);
	}

	public ModelAndView getInsert(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Position p = new Position();
			bind(request, p);
			Company c = (Company) request.getSession().getAttribute("company");
			p.setCompany(c);
			if (p.getId() == 0)
				getUniversalManager().insert(p);
			else
				getUniversalManager().update(p);
			getMessage(response, "{\"success\":true,\"err\":\"\"}");
		} catch (Exception e) {
			e.printStackTrace();
			getMessage(response, "{\"success\":false,\"err\":\""
					+ e.getMessage() + "\"}");
		}
		return null;
	}

	public ModelAndView queryList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map param = new HashMap();
		Map result = new HashMap();
		Position p = (Position) getObjectFromJson(request, response, this.obj);
		Page page = getUserManager().getPositionDao().queryList(
				this.getPageNo(request), pageSize, p);
		result.put("o", page);
		return new ModelAndView("company/queryPositionlist", result);
	}

	public ModelAndView topPositionList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		Position p = new Position();
		String top = request.getParameter("top");
		String type = request.getParameter("type");
		int top_int = 10;
		if (top != null && !"".equals(top))
			top_int = Integer.parseInt(top);
		List list = null;
		if ("new".equals(type))
			list = getUserManager().getPositionDao().topNewPosition(top_int);
		else if ("view".equals(type))
			list = getUserManager().getPositionDao().topViewPosition(top_int);
		else if ("fix".equals(type))
			list = getUserManager().getPositionDao().topFixPosition(top_int);
		result.put("o", list);
		return new ModelAndView("company/topPositionlist", result);
	}

	public ModelAndView myList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map param = new HashMap();
		Map result = new HashMap();
		Company c = (Company) request.getSession().getAttribute("company");
		Position p = new Position();
		bind(request, p);
		p.setCompany(c);
		Page page = getUserManager().getPositionDao().queryList(
				this.getPageNo(request), pageSize, p);
		result.put("o", page);
		return new ModelAndView("company/myPositionlist", result);
	}

	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) {
		User u = (User) request.getSession().getAttribute("user");
		Map result = new HashMap();
		String id = request.getParameter("id");
		Position p = new Position();
		p.setId(Integer.parseInt(id));
		Page page = getUserManager().getPositionDao().queryList(1, 1, p);
		if (page.getDataList().size() == 1)
			result.put("o", page.getDataList().get(0));
		if (u != null) {
			UPHistory uph = new UPHistory();
			uph.setPositionid(p.getId());
			uph.setUserid(u.getId());
			getUniversalManager().insert(uph);
		}
		return new ModelAndView("company/viewPosition", result);
	}

	public ModelAndView getDelete(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
//		Position p = null;
		Admin a = (Admin) request.getSession().getAttribute(Const.ADMIN);
		Company c = (Company) request.getSession().getAttribute(Const.COMPANY);
		if (id != null) {
			try {
//				p = (Position) getUniversalManager().findByID(Position.class,
//						Integer.parseInt(id));
				if (c != null && id!=null)
					companyManager.getPositionDao().delete(Integer.parseInt(id));
				else if (a != null)
					companyManager.getPositionDao().delete(Integer.parseInt(id));
				getMessage(response, "{\"success\":true,\"err\":\"\"}");
			} catch (Exception e) {
				e.printStackTrace();
				getMessage(response, "{\"success\":false,\"err\":\""
						+ e.getMessage() + "\"}");
			}
		}
		return null;
	}

	public ModelAndView getFixPosition(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		String fix = request.getParameter("fix");
		try {
			if (fix == null || "".equals(fix))
				getCompanyManager().getPositionDao().fix(Integer.parseInt(id),
						1);
			else
				getCompanyManager().getPositionDao().fix(Integer.parseInt(id),
						Integer.parseInt(fix));
			getMessage(response, "{\"success\":true,\"err\":\"\"}");
		} catch (Exception e) {
			e.printStackTrace();
			getMessage(response, "{\"success\":false,\"err\":\""
					+ e.getMessage() + "\"}");
		}
		return null;
	}
}
